package com.example.util;

import com.example.annotation.MyAuthority;
import com.example.exception.LoginException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;

//@Component
@Aspect
public class AopUtil {

    @Before("@annotation(requestMapping)")
    public void beforeDoJob(JoinPoint joinPoint, RequestMapping requestMapping)
    {
        //Class controller = joinPoint.getTarget().getClass();注解所在类
        MethodSignature methodSignature =(MethodSignature)joinPoint.getSignature();
        Method method = methodSignature.getMethod();//注解所在方法
        //刚刚还想找到注解所在成员变量Field呢，不可以，aop是对方法的切面，不是对类和成员变量的切面，哈哈
        MyAuthority myAuthority = method.getAnnotation(MyAuthority.class);
        if (null==myAuthority)
        {
//            if(null==LoginHelper.getUserInfo())
//                LoginException.createException("1112");
        }
        else
        {
            switch (myAuthority.value())
            {
                case ANONYMOUSLY:
                    if(null!=LoginHelper.getUserInfo())
                        throw  LoginException.createException("1113");
                    break;
                case IS_LOGIN:
                    if(null==LoginHelper.getUserInfo())//死的心都有了。。。
                        throw  LoginException.createException("1112");
                    break;
            }
        }
    }
}
